package com.example.lenovo.downloaddemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.fragment.CartFragment;
import com.example.lenovo.downloaddemo.fragment.CategoryFragment;
import com.example.lenovo.downloaddemo.fragment.HomeFragment;
import com.example.lenovo.downloaddemo.fragment.PersonalFragment;
import com.example.lenovo.downloaddemo.fragment.SearchFragment;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.util.LanguagePreUtils;
import com.example.lenovo.downloaddemo.util.StatusBarUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.facebook.drawee.backends.pipeline.Fresco;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    public static final String TAG = MainActivity.class.getSimpleName();
    @Bind(R.id.frameLayout)
    FrameLayout frameLayout;
    @Bind(R.id.radio_group0)
    LinearLayout radioGroup0;
    @Bind(R.id.radio_group1)
    LinearLayout radioGroup1;

    @Bind(R.id.radio_group2)
    LinearLayout radioGroup2;
    //搜索
    @Bind(R.id.radio_group_2)
    LinearLayout radio_group_2;
    @Bind(R.id.radio_group3)
    LinearLayout radioGroup3;
    @Bind(R.id.radioGroup)
    LinearLayout radioGroup;
    @Bind(R.id.index_homeText)
    TextView indexHomeText;
    @Bind(R.id.index_categoryText)
    TextView indexCategoryText;
    @Bind(R.id.index_cartText)
    TextView indexCartText;
    @Bind(R.id.index_searchImg)
    ImageView indexSearchImg;
    @Bind(R.id.index_searchText)
    TextView indexSearchText;
    @Bind(R.id.index_centerText)
    TextView indexCenterText;
    @Bind(R.id.index_homeImg)
    ImageView indexHomeImg;
    @Bind(R.id.index_categoryImg)
    ImageView indexCategoryImg;
    @Bind(R.id.index_cartImg)
    ImageView indexCartImg;
    @Bind(R.id.index_centerImg)
    ImageView indexCenterImg;

    public Fragment currentFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Fresco.initialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this);

        initView();
        initListener();
    }


    public void initView() {
//        TitleBarView titleBarView=new TitleBarView(MainActivity.this);
        frameLayout = findViewById(R.id.frameLayout);
        //设置样式 默认选中第一个
        onClick(radioGroup0);
        indexSearchText.setText(AppContext.getInstance().getResources().getText(R.string.search));
        indexCenterText.setText(AppContext.getInstance().getResources().getText(R.string.center));
        indexCartText.setText(AppContext.getInstance().getResources().getText(R.string.paihangbang));
        indexCategoryText.setText(AppContext.getInstance().getResources().getText(R.string.fenlei));
        indexHomeText.setText(AppContext.getInstance().getResources().getText(R.string.tuijian));
//        LanguagePreUtils.setStringToPre(instance,
//                "language", "EN");
    }

    public void initListener() {
        //设置点击事件
        radioGroup0.setOnClickListener(this);
        radioGroup1.setOnClickListener(this);
        radioGroup2.setOnClickListener(this);
        radio_group_2.setOnClickListener(this);
        radioGroup3.setOnClickListener(this);
    }

    //
    private long exitTime = 0;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (null != currentFragment && currentFragment instanceof HomeFragment) {
//                exit();
//                finish();
                currentFragment = null ;
                HomeFragment.instance = null ;
                CategoryFragment.instance = null ;
                CartFragment.instance = null ;
                SearchFragment.instance=null;
                PersonalFragment.instance = null ;
                onBackPressed();
            } else {
                onClick(radioGroup0);
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }


    @Override
    public void onClick(View v) {
        FragmentManager fManager = getSupportFragmentManager();
        FragmentTransaction ft = fManager.beginTransaction();
        //设置默认颜色
        indexHomeText.setTextColor(getResources().getColor(R.color.color_333));
        indexCategoryText.setTextColor(getResources().getColor(R.color.color_333));
        indexCartText.setTextColor(getResources().getColor(R.color.color_333));
        indexSearchText.setTextColor(getResources().getColor(R.color.color_333));
        indexCenterText.setTextColor(getResources().getColor(R.color.color_333));
        // 设置默认图
        indexHomeImg.setImageResource(R.mipmap.recommend_gray);

        indexCategoryImg.setImageResource(R.mipmap.category_gray);

        indexCartImg.setImageResource(R.mipmap.rank_gray);
        indexSearchImg.setImageResource(R.mipmap.seek_gray);
        indexCenterImg.setImageResource(R.mipmap.update_gray);

        switch (v.getId()) {
            case R.id.radio_group0:
                if (null != HomeFragment.getInstance()) {
                    ft.replace(R.id.frameLayout, HomeFragment.getInstance());
                } else {
                    ft.replace(R.id.frameLayout, new HomeFragment());
                }

                indexHomeText.setTextColor(Color.parseColor("#77a7da"));
                indexHomeImg.setImageResource(R.mipmap.recommend_blue);
                currentFragment = HomeFragment.getInstance();
                ft.commit();
                break;
            case R.id.radio_group1:
                if (null != CategoryFragment.getInstance()) {
                    ft.replace(R.id.frameLayout, CategoryFragment.getInstance());
                } else {
                    ft.replace(R.id.frameLayout, new CategoryFragment());
                }

                indexCategoryText.setTextColor(Color.parseColor("#77a7da"));
                indexCategoryImg.setImageResource(R.mipmap.category_blue);
                currentFragment = CategoryFragment.getInstance();
                ft.commit();
                break;
            case R.id.radio_group2:
                if (null != CartFragment.getInstance()) {
                    ft.replace(R.id.frameLayout, CartFragment.getInstance());
                } else {
                    ft.replace(R.id.frameLayout, new CartFragment());
                }

                indexCartText.setTextColor(Color.parseColor("#77a7da"));
                indexCartImg.setImageResource(R.mipmap.rank_blue);
                currentFragment = CartFragment.getInstance();
                ft.commit();
                break;
            case R.id.radio_group3:
                if (null != PersonalFragment.getInstance()) {
                    ft.replace(R.id.frameLayout, PersonalFragment.getInstance());
                } else {
                    ft.replace(R.id.frameLayout, new PersonalFragment());
                }

                indexCenterText.setTextColor(Color.parseColor("#77a7da"));
                indexCenterImg.setImageResource(R.mipmap.update_blue);
                currentFragment = PersonalFragment.getInstance();
                ft.commit();
                break;
            case R.id.radio_group_2:
                if (null != SearchFragment.getInstance()) {
                    ft.replace(R.id.frameLayout, SearchFragment.getInstance());
                } else {
                    ft.replace(R.id.frameLayout, new SearchFragment());
                }

                indexSearchText.setTextColor(Color.parseColor("#77a7da"));
                indexSearchImg.setImageResource(R.mipmap.seek_blue);
                currentFragment = SearchFragment.getInstance();
                ft.commit();
                break;
        }
    }
}
